library(recommenderlab)
library(stats)

DataTable <- read.table("ml-100k/u.data")

MovieLense <- as(DataTable, "realRatingMatrix")

MovieLense

rowc <- rowCounts(MovieLense)
median(rowc)

# RANDOM

schemaRND1 <- evaluationScheme(MovieLense, method="split", train=0.9, given=15, goodRating=3)
rRND1 <- Recommender(getData(schemaRND1, "train"), method = "RANDOM")

system.time(prRND1 <- predict(rRND1,getData(schemaRND1,"known"),type="ratings"))


schemaRND2 <- evaluationScheme(MovieLense, method="split", train=0.8, given=15, goodRating=3)
rRND2 <- Recommender(getData(schemaRND2, "train"), method = "RANDOM")
system.time(prRND2 <- predict(rRND2,getData(schemaRND2,"known"),type="ratings"))

schemaRND3 <- evaluationScheme(MovieLense, method="split", train=0.7, given=15, goodRating=3)
rRND3 <- Recommender(getData(schemaRND3, "train"), method = "RANDOM")
system.time(prRND3 <- predict(rRND3,getData(schemaRND3,"known"),type="ratings"))

# SVD

schema <- evaluationScheme(MovieLense, method="split", train=0.9, given=15, goodRating=3)

rSVD1 <- Recommender(getData(schema, "train"), method = "SVD", parameter=list(method="cosine", normalize="center", categories=50, treat_na="median"))
rSVD2 <- Recommender(getData(schema, "train"), method = "SVD", parameter=list(method="cosine", normalize="Z-score", categories=50, treat_na="median"))
rSVD3 <- Recommender(getData(schema, "train"), method = "SVD", parameter=list(method="pearson", normalize="center", categories=50, treat_na="median"))
rSVD4 <- Recommender(getData(schema, "train"), method = "SVD", parameter=list(method="pearson", normalize="Z-score", categories=50, treat_na="median"))

rSVD5 <- Recommender(getData(schema, "train"), method = "SVD", parameter=list(method="cosine", normalize="center", categories=100, treat_na="median"))
rSVD6 <- Recommender(getData(schema, "train"), method = "SVD", parameter=list(method="cosine", normalize="Z-score", categories=100, treat_na="median"))
rSVD7 <- Recommender(getData(schema, "train"), method = "SVD", parameter=list(method="pearson", normalize="center", categories=100, treat_na="median"))
rSVD8 <- Recommender(getData(schema, "train"), method = "SVD", parameter=list(method="pearson", normalize="Z-score", categories=100, treat_na="median"))

# Dodatkowe sprwdzanie przy traktowaniu NA jako 0
rSVD9 <- Recommender(getData(schema, "train"), method = "SVD", parameter=list(method="cosine", normalize="center", categories=50, treat_na="0"))
rSVD10 <- Recommender(getData(schema, "train"), method = "SVD", parameter=list(method="cosine", normalize="Z-score", categories=50, treat_na="0"))
rSVD11 <- Recommender(getData(schema, "train"), method = "SVD", parameter=list(method="pearson", normalize="center", categories=50, treat_na="0"))
rSVD12 <- Recommender(getData(schema, "train"), method = "SVD", parameter=list(method="pearson", normalize="Z-score", categories=50, treat_na="0"))

system.time(prSVD1 <- predict(rSVD1,getData(schema,"known"),type="ratings"))
system.time(prSVD2 <- predict(rSVD2,getData(schema,"known"),type="ratings"))
system.time(prSVD3 <- predict(rSVD3,getData(schema,"known"),type="ratings"))
system.time(prSVD4 <- predict(rSVD4,getData(schema,"known"),type="ratings"))
system.time(prSVD5 <- predict(rSVD5,getData(schema,"known"),type="ratings"))
system.time(prSVD6 <- predict(rSVD6,getData(schema,"known"),type="ratings"))
system.time(prSVD7 <- predict(rSVD7,getData(schema,"known"),type="ratings"))
system.time(prSVD8 <- predict(rSVD8,getData(schema,"known"),type="ratings"))
system.time(prSVD9 <- predict(rSVD9,getData(schema,"known"),type="ratings"))
system.time(prSVD10 <- predict(rSVD10,getData(schema,"known"),type="ratings"))
system.time(prSVD11 <- predict(rSVD11,getData(schema,"known"),type="ratings"))
system.time(prSVD12 <- predict(rSVD12,getData(schema,"known"),type="ratings"))

# UBCF

rUBCF1 <- Recommender(MovieLense, method = "UBCF", parameter=list(method="cosine", nn=10, normalize="center"))
rUBCF2 <- Recommender(MovieLense, method = "UBCF", parameter=list(method="pearson", nn=10, normalize="center"))
rUBCF3 <- Recommender(MovieLense, method = "UBCF", parameter=list(method="cosine", nn=25, normalize="center"))
rUBCF4 <- Recommender(MovieLense, method = "UBCF", parameter=list(method="pearson", nn=25, normalize="center"))
rUBCF5 <- Recommender(MovieLense, method = "UBCF", parameter=list(method="cosine", nn=10, normalize="Z-score"))
rUBCF6 <- Recommender(MovieLense, method = "UBCF", parameter=list(method="pearson", nn=10, normalize="Z-score"))
rUBCF7 <- Recommender(MovieLense, method = "UBCF", parameter=list(method="cosine", nn=25, normalize="Z-score"))
rUBCF8 <- Recommender(MovieLense, method = "UBCF", parameter=list(method="pearson", nn=25, normalize="Z-score"))

system.time(prUBCF1 <- predict(rUBCF1,getData(schema,"known"),type="ratings"))
system.time(prUBCF2 <- predict(rUBCF2,getData(schema,"known"),type="ratings"))
system.time(prUBCF3 <- predict(rUBCF3,getData(schema,"known"),type="ratings"))
system.time(prUBCF4 <- predict(rUBCF4,getData(schema,"known"),type="ratings"))
system.time(prUBCF5 <- predict(rUBCF5,getData(schema,"known"),type="ratings"))
system.time(prUBCF6 <- predict(rUBCF6,getData(schema,"known"),type="ratings"))
system.time(prUBCF7 <- predict(rUBCF7,getData(schema,"known"),type="ratings"))
system.time(prUBCF8 <- predict(rUBCF8,getData(schema,"known"),type="ratings"))


error <- rbind(
        calcPredictionError(prRND1, getData(schemaRND1,"unknown")),
        calcPredictionError(prRND2, getData(schemaRND2,"unknown")),
        calcPredictionError(prRND3, getData(schemaRND3,"unknown")),
        calcPredictionError(prSVD1, getData(schema,"unknown")),
        calcPredictionError(prSVD2, getData(schema,"unknown")),
        calcPredictionError(prSVD3, getData(schema,"unknown")),
        calcPredictionError(prSVD4, getData(schema,"unknown")),
        calcPredictionError(prSVD5, getData(schema,"unknown")),
        calcPredictionError(prSVD6, getData(schema,"unknown")),
        calcPredictionError(prSVD7, getData(schema,"unknown")),
        calcPredictionError(prSVD8, getData(schema,"unknown")),
        calcPredictionError(prSVD9, getData(schema,"unknown")),
        calcPredictionError(prSVD10, getData(schema,"unknown")),
        calcPredictionError(prSVD11, getData(schema,"unknown")),
        calcPredictionError(prSVD12, getData(schema,"unknown")),
        calcPredictionError(prUBCF1, getData(schema,"unknown")),
        calcPredictionError(prUBCF2, getData(schema,"unknown")),
        calcPredictionError(prUBCF3, getData(schema,"unknown")),
        calcPredictionError(prUBCF4, getData(schema,"unknown")),
        calcPredictionError(prUBCF5, getData(schema,"unknown")),
        calcPredictionError(prUBCF6, getData(schema,"unknown")),
        calcPredictionError(prUBCF7, getData(schema,"unknown")),
        calcPredictionError(prUBCF8, getData(schema,"unknown"))
)

rownames(error) <- c(
        "RANDOM1 recommender(train=0.9) = ",
        "RANDOM2 recommender(train=0.8) = ",
        "RANDOM3 recommender(train=0.7) = ",
        "SVD1 recommender(train=0.9) = ",
        "SVD2 recommender(train=0.9) = ",
        "SVD3 recommender(train=0.9) = ",
        "SVD4 recommender(train=0.9) = ",
        "SVD5 recommender(train=0.9) = ",
        "SVD6 recommender(train=0.9) = ",
        "SVD7 recommender(train=0.9) = ",
        "SVD8 recommender(train=0.9) = ",
        "SVD9 recommender(train=0.9) = ",
        "SVD10 recommender(train=0.9) = ",
        "SVD11 recommender(train=0.9) = ",
        "SVD12 recommender(train=0.9) = ",
        "UBCF1 recommender(train=0.9) = ",
        "UBCF2 recommender(train=0.9) = ",
        "UBCF3 recommender(train=0.9) = ",
        "UBCF4 recommender(train=0.9) = ",
        "UBCF5 recommender(train=0.9) = ",
        "UBCF6 recommender(train=0.9) = ",
        "UBCF7 recommender(train=0.9) = ",
        "UBCF8 recommender(train=0.9) = "
)

error
